#ifndef HTRIGGER_H_INCLUDED
#define HTRIGGER_H_INCLUDED

/* AddProximityTrigger(uint size) */
int AddProximityTrigger(lua_State* l) {
	uint n = lua_gettop(l);
	if (n==1 && isnum(1)) {
		cProximityTrigger* ptr = new cProximityTrigger(P.Scene()->addCubeSceneNode(tonum(1)));

		if (DEBUG)
    		ptr->getBody()->setMaterialFlag(EMF_WIREFRAME,true);
    	else {
			ptr->getBody()->setMaterialFlag(EMF_BACK_FACE_CULLING, true);
			ptr->getBody()->setMaterialFlag(EMF_FRONT_FACE_CULLING, true);
    	}

		uint id = P.Tmgr.addTrigger(ptr);

		printf("Created proximity trigger with id %d\n", id);

		lua_pushnumber(l, id);
		return 1;
	} else {
		return -1;
	}
}

/* SetTriggerPos(uint id, num x, num y, num z) */
int SetTriggerPos(lua_State* l) {
	uint n = lua_gettop(l), id;
	vector3df newpos;
	if (n >= 1 && isnum(1)) id = tonum(1);
	if (n >= 2 && isnum(2)) newpos.X = tonum(2);
	if (n >= 3 && isnum(3)) newpos.Y = tonum(3);
	if (n == 4 && isnum(4)) newpos.Z = tonum(4);
    P.Tmgr.getProximityTrigger(id)->getBody()->setPosition(newpos);
    printf("Set trigger %d pos to %f/%f/%f\n", id , newpos.X, newpos.Y, newpos.Z);
	return 0;
}

/* SetTriggerScale(uint id, num x, num y, num z) */
int SetTriggerScale(lua_State* l) {
	uint n = lua_gettop(l), id;
	vector3df newscale;
	if (n >= 1 && isnum(1)) id = tonum(1);
	if (n >= 2 && isnum(2)) newscale.X = tonum(2);
	if (n >= 3 && isnum(3)) newscale.Y = tonum(3);
	if (n == 4 && isnum(4)) newscale.Z = tonum(4);
    P.Tmgr.getProximityTrigger(id)->getBody()->setScale(newscale);
    printf("Set trigger %d scale to %f/%f/%f\n", id , newscale.X, newscale.Y, newscale.Z);
	return 0;
}

/* AddTriggerFoo(uint id, uint type, string foo, table params) */
int AddTriggerFoo(lua_State* l) {
	uint n = lua_gettop(l), id, type;
	stringc foo, param_table;
	if (n >= 1 && isnum(1)) id = tonum(1);
	if (n >= 2 && isnum(2)) type = tonum(2);
	if (n >= 3 && isstr(3)) foo = stringc(tostr(3));
	if (n == 4 && lua_istable(l,4)) {
	param_table.append(foo).append(stringc(type)).append(stringc(id));
        lua_setglobal(l, param_table.c_str());
	}

	cProximityTrigger* ptr = P.Tmgr.getProximityTrigger(id);

	printf("Trying add func \"%s\" to proximity trigger %d event %d with params table %s\n", foo.c_str(), id, type, param_table.c_str());

	if (ptr) {
		ptr->addAction(type, [=]{
							//get function
							lua_getglobal(l, foo.c_str());
							lua_getglobal(l, param_table.c_str());
							if(lua_pcall(l,1,1,0) != 0)
								printf("error running function : %s\n", lua_tostring(l, -1));

							lua_pop(l, 1);
						});
		printf("Foo attached to trigger\n");
	} else {
		return LOG_ERROR("AddTriggerFoo: trigger not found!");
	}

	return 0;
}

int ActivateTrigger(lua_State* l) {
	if (!lua_gettop(l) || !isnum(1)) return LOG_ERROR("ActivateTrigger: no parameter found!");
	uint id = tonum(1);
	P.Tmgr.getProximityTrigger(id)->Activate();
	return 0;
}

int DeactivateTrigger(lua_State* l) {
	if (!lua_gettop(l) || !isnum(1)) return LOG_ERROR("ActivateTrigger: no parameter found!");
	uint id = tonum(1);
	P.Tmgr.getProximityTrigger(id)->Deactivate();
	return 0;
}

void initTriggerFuncs() {
	REG(AddProximityTrigger);
	REG(SetTriggerPos);
	REG(SetTriggerScale);
	REG(AddTriggerFoo);
	REG(ActivateScene);
	REG(DeactivateTrigger);
}

#endif // HTRIGGER_H_INCLUDED
